package org.zxy.auth.controller;

import org.zxy.auth.context.LoginContext;
import org.zxy.auth.service.IMenuService;
import org.zxy.auth.domain.Menu;
import org.zxy.auth.query.MenuQuery;
import org.zxy.base.annotation.RonghuaPermission;
import org.zxy.base.util.AjaxResult;
import org.zxy.base.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zxy.org.domain.Employee;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/menu")
@RonghuaPermission(name = "菜单管理")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改
     *
     * @param menu 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @RonghuaPermission(name = "菜单的增加和修改")
    public AjaxResult insertOrUpdate(@RequestBody Menu menu) {
        try {
            if (menu.getId() != null)
                menuService.update(menu);
            else
                menuService.insert(menu);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @RonghuaPermission(name = "根据Id删除菜单")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        try {
            menuService.deleteById(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 根据Id查询对象信息
     *
     * @param
     * @return
     */
    //获取用户
    @GetMapping("/{id}")
    @RonghuaPermission(name = "根据Id查询菜单")
    public AjaxResult selectById(@PathVariable("id") Long id) {
        try {
            Menu menu = menuService.selectById(id);
            return AjaxResult.success(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有对象信息
     *
     * @return
     */
    @GetMapping
    @RonghuaPermission(name = "查询所有菜单")
    public AjaxResult selectAll() {

        try {
            List<Menu> list = menuService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    @RonghuaPermission(name = "菜单分页查询")
    public AjaxResult queryPage(@RequestBody MenuQuery query) {
        try {
            PageList<Menu> pageList = menuService.queryPage(query);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除对象
     *
     * @param
     * @return
     */
    @PatchMapping
    @RonghuaPermission(name = "菜单批量查询")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            menuService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败！" + e.getMessage());
        }
    }
    //查询用户权限
    @GetMapping("/byEmplopyee")
    public AjaxResult listByEmplopyee(HttpServletRequest request){
        try {
            Employee employee = LoginContext.getEmployee(request);
            List<Menu> list = menuService.byEmplopyee(employee.getId());
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败！" + e.getMessage());
        }
    }
}
